home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xpat2-1.000 / xpat2-1 / xpat2-1.04 / src / Xm-main.c < prev    next >
C/C++ Source or Header  |  1995-11-25  |  12KB  |  377 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    X patience version 2 -- module Xm-main.c                 */
  5. /*                                         */
  6. /*    main function for the Athena Widget interface                 */
  7. /*    written by Heiko Eissfeldt and Michael Bischoff                 */
  8. /*    March-1994                                 */
  9. /*    see COPYRIGHT.xpat2 for Copyright details                 */
  10. /*                                         */
  11. /*                                         */
  12. /*****************************************************************************/
  13. #ifdef useMotif
  14. #ifdef ONLINE_HELP
  15. #undef ONLINE_HELP    /* not here yet */
  16. #endif
  17.  
  18. #include "X-pat.h"
  19. #include "Tableau.h"
  20. #include "version.h"
  21.  
  22. #include <Xm/CascadeB.h>
  23. #include <Xm/ToggleB.h>
  24. #include <Xm/MainW.h>
  25. #include <Xm/MessageB.h>
  26.  
  27. void create_help(Widget);
  28. void popup_help(Widget, XtPointer, XtPointer);
  29. void popdown_help(Widget, XtPointer, XtPointer);
  30.  
  31. Window table, mainwindow;
  32. Widget toplevel;
  33. static Widget messagebox, container, desktop, sbh, sbv;
  34. static void perform_command(Widget widget, XtPointer client_data, XtPointer call_data) {
  35.     (*(void (*)(void))client_data)();    /* any questions? */
  36. }
  37.  
  38. void show_message(const char *str, ...) {
  39.     XmString str2;
  40.     static char last_message[256];
  41.     Arg Args;
  42.     va_list args;
  43.     va_start(args, str);
  44.  
  45.     if (!str) {
  46.     memset(last_message, ' ', sizeof(last_message)-1);
  47.     last_message[sizeof(last_message)-1] = '\0';
  48.     } else
  49.     vsprintf(last_message, str, args);
  50.  
  51.     str2 = XmStringCreateSimple(last_message);
  52.     XtSetArg(Args, XmNlabelString, str2);
  53.     XtSetValues(messagebox, &Args, 1);
  54. }
  55.  
  56.  
  57.  
  58. static XtAppContext app_con;
  59. static void (*execfunc)(void) = NULL;
  60. static Widget dialog;
  61.  
  62. void cmd_LeavePat(void) {
  63.     play_sound("goodbye");
  64.     XtDestroyApplicationContext(app_con);
  65.     exit(0);
  66. }
  67.  
  68. static void popup_confirm(const char *prompt) {
  69.     Arg Args[2];
  70.     XmString str2;
  71.     str2 = XmStringCreateSimple(prompt);
  72.     XtSetArg(Args[0], XmNmessageString, str2);
  73.     XtSetValues(dialog, Args, 1);
  74.     XtManageChild(dialog);
  75. }
  76.  
  77. static void Cancel(Widget widget, XtPointer client_data, XtPointer call_data) {
  78.     XtUnmanageChild(dialog);
  79.     execfunc = NULL;
  80. }
  81. static void Ok(Widget widget, XtPointer client_data, XtPointer call_data) {
  82.     (*execfunc)();    /* execute the desired function */
  83.     Cancel(NULL, dialog, NULL);
  84. }
  85. void cmd_Cancel(void) {
  86.     if (execfunc)
  87.     Cancel(NULL, dialog, NULL);
  88. }
  89. void cmd_Confirm(void) {
  90.     if (execfunc) {
  91.     (*execfunc)();    /* execute the desired function */
  92.     Cancel(NULL, dialog, NULL);
  93.     }
  94. }
  95.  
  96. void request_confirm(void (*dofunc)(void), const char *prompt) {
  97.     if (execfunc)
  98.     return;        /* request pending => deny another one */
  99.     execfunc = dofunc;
  100.     popup_confirm(prompt);
  101. }
  102.  
  103. static String fallback_resources[] = { 
  104.     /* will be filled out later */
  105.     NULL,
  106. };
  107.  
  108. static XrmOptionDescRec options[] = {
  109.     /* tableau resources */
  110.     { "-xpmdir",    "*Tableau.xpmdir",    XrmoptionSepArg, NULL },
  111. #ifdef LOADCARDS
  112.     { "-cards",        "*Tableau.cardset",    XrmoptionSepArg, NULL },
  113. #endif
  114.     { "-rules",        "*Tableau.rules",    XrmoptionSepArg, NULL },
  115.     { "-faceup",    "*Tableau.faceup",    XrmoptionSepArg, NULL },
  116.     { "-facedown",    "*Tableau.facedown",    XrmoptionSepArg, NULL },
  117.     { "-decks",        "*Tableau.decks",    XrmoptionSepArg, NULL },
  118.     { "-slots",     "*Tableau.slots",    XrmoptionSepArg, NULL },
  119.     { "-jokers",    "*Tableau.jokers",    XrmoptionSepArg, NULL },
  120.     { "-tmps",        "*Tableau.tmps",    XrmoptionSepArg, NULL },
  121.     { "-p0",            "*Tableau.param0",    XrmoptionSepArg, NULL },
  122.     { "-p1",            "*Tableau.param1",    XrmoptionSepArg, NULL },
  123.     { "-p2",        "*Tableau.param2",    XrmoptionSepArg, NULL },
  124.     { "-p3",        "*Tableau.param3",    XrmoptionSepArg, NULL },
  125.     { "-cb",            "*Tableau.cardbackColor",XrmoptionSepArg,NULL },
  126.     { "-tb",            "*Tableau.background",    XrmoptionSepArg,NULL },
  127.     { "-red",            "*Tableau.RedColor",    XrmoptionSepArg, NULL },
  128.     { "-markcolor",    "*Tableau.markColor",    XrmoptionSepArg, NULL },
  129.     { "-arrowcolor",    "*Tableau.arrowColor",    XrmoptionSepArg, NULL },
  130.  
  131.     /* non-tableau resources */
  132. #ifdef SOUND
  133.     { "-sound",     "*Sound.state",          XrmoptionNoArg, (XtPointer)"True" },
  134.     { "-nosound",     "*Sound.state",          XrmoptionNoArg, (XtPointer)"False" },
  135. #endif
  136. };
  137.  
  138. static void HintNotify(Widget w, XEvent *xev, String *params, Cardinal *num) {
  139.     /* check to be sure state is still Set so that user can cancel
  140.        the action (e.g. by moving outside the window, in the default
  141.        bindings. */
  142.     /* Nope, we cannot do this, the 'set' state is private data */
  143.     /* CommandWidget cbw = (CommandWidget)w; 
  144.     if (cbw->command.set)
  145.     XtCallCallbackList(w, cbw->command.callbacks, (XtPointer) NULL); */
  146.     switch (((XButtonPressedEvent *)xev)->button) {
  147.     case Button1:
  148.     cmd_NextHint();
  149.     break;
  150.     case Button2:
  151.     cmd_DoHint();
  152.     break;
  153.     case Button3:
  154.     cmd_PreviousHint();
  155.     }
  156. }
  157.  
  158. /* type converter functions: */
  159. static void mXtAP_Cancel (Widget w, XEvent *xev, String *params, Cardinal *num) { cmd_Cancel(); }
  160. static void mXtAP_Confirm(Widget w, XEvent *xev, String *params, Cardinal *num) { cmd_Confirm(); }
  161.  
  162. static XtActionsRec moreActions[] = {
  163.     { "HintNotify",    HintNotify },
  164.     { "Cancel",        mXtAP_Cancel },
  165.     { "Ok",        mXtAP_Confirm }
  166. };
  167.  
  168. char *loadfilename;
  169.  
  170. static void process_extra_args(int argc, char *argv[]) {
  171.     /* check extra args */
  172.     game.seed = -1L;
  173.     loadfilename = NULL;
  174.     if (argc > 2) {
  175.     usage:
  176.     fprintf(stderr, "xpat: invalid argument: %s\n", argv[1]);
  177.     exit(EXIT_FAILURE);
  178.     } else if (argc == 2) {
  179.     /* seed or load file */
  180.     if (*argv[1] == '-')
  181.         goto usage;
  182.     else if (isdigit(*argv[1])) {
  183.         game.seed = atol(argv[1]);
  184.         if (game.seed < 0L || game.seed >= 1000000000L)
  185.         goto usage;
  186.     } else {
  187.         loadfilename = argv[1];
  188.         /* printf("loadfile is %s\n", loadfilename); */
  189.     }
  190.     }
  191. }
  192.  
  193. /*extern void cmd_DebugOut(void); */
  194. extern struct rules *rulepool[];
  195. static void selectrules(Widget w, XtPointer number, XtPointer garbage) {
  196.     const char *s = XtName(w);
  197.     /* printf("widget %s has been selected\n", s); */
  198.     change_rules(s);
  199. }
  200.  
  201. #ifdef SOUND
  202. static Widget sound;
  203.  
  204. int checksound(void) {
  205.     return XmToggleButtonGetState(sound);
  206. }
  207. #endif
  208.  
  209. static Widget paned;
  210.  
  211. int main(int argc, char *argv[]) {
  212.     Widget buttonpanel, main;
  213.     Widget gamebutton, gamemenu, rulesbutton, rulesmenu, coloursbutton;
  214.     int i;
  215.     Arg Args[4];
  216.     struct button {
  217.     const char *name; void (*func)(void);
  218.     } *bp;
  219.     static struct button buttons[] = {
  220.     { "Cards",          cmd_DealCards },
  221.     { "Undo",          cmd_UndoMove },
  222.     { "Redo",          cmd_RedoMove },
  223.     { "Score",          cmd_ShowScore },
  224.     { "Hint",          cmd_NextHint },
  225.     { "Move to Stack",    cmd_ToStack },
  226.     /* { "Debug",        cmd_DebugOut }, */
  227. #ifdef ONLINE_HELP
  228.     { "Help",          popup_help },
  229. #endif
  230.     { "Save",          cmd_SaveGame }
  231.     }, mbuttons[] = {
  232.     { "Drop Bookmark",    cmd_DropBookmark },
  233.     { "Goto Bookmark",    cmd_GotoBookmark },
  234.     { "Replay",        cmd_ReplayGame },
  235.     { "Restart",        cmd_RestartGame },
  236.     { "New Game",        rq_AnotherGame },
  237.     { "Quit",        rq_LeavePat }
  238.     };
  239.  
  240.     /* use the command line arguments concerning the widgets */
  241.     switch_uid(1);
  242.     toplevel = XtAppInitialize(&app_con, "XPat", options, XtNumber(options), &argc, argv,
  243.                    fallback_resources, NULL, 0);
  244.     switch_uid(0);
  245.     process_extra_args(argc, argv);
  246.     XtAppAddActions(app_con, moreActions, XtNumber(moreActions));
  247.     graphics_control(Disable);
  248.  
  249.  
  250.     /* ab hier neu */
  251.  
  252.         main = XmCreateMainWindow (toplevel, "main", NULL, 0);
  253.         XtManageChild (main);
  254.  
  255.     /* basic elements */
  256.     paned       = XtCreateManagedWidget("paned",    xmPanedWindowWidgetClass,    main,      NULL, 0);
  257. /*    XtSetArg(args[0], XmNorientation, XmHORIZONTAL); */
  258.     buttonpanel = XtCreateManagedWidget("buttonpanel",    xmRowColumnWidgetClass,     paned,      NULL, 0);
  259.     messagebox = XtCreateManagedWidget("messages", xmLabelWidgetClass, paned, NULL, 0);
  260.     show_message(" ");
  261.  
  262.     container   = XtCreateManagedWidget("container",    xmScrolledWindowWidgetClass, paned,      NULL, 0);
  263.     desktop     = XtCreateManagedWidget("desktop",    tableauWidgetClass,  container,      NULL, 0);
  264.  
  265.  
  266.     /* create the button panel and its menus */
  267.     gamebutton  = XtCreateManagedWidget("Game", xmCascadeButtonWidgetClass, buttonpanel, NULL, 0);
  268.     gamemenu    = XmCreatePulldownMenu(buttonpanel, "gamemenu", NULL, 0);
  269.     for (bp = mbuttons, i = 0; i < XtNumber(mbuttons); ++i) {
  270.     Widget w;
  271.     w = XtCreateManagedWidget(bp->name, xmPushButtonWidgetClass, gamemenu, NULL, 0);
  272.     if (bp->func) XtAddCallback(w, XmNactivateCallback, perform_command, (XtPointer)bp->func);
  273.     ++bp;
  274.     }
  275.     XtVaSetValues(gamebutton, XmNsubMenuId, gamemenu, NULL);
  276.  
  277.     /* create Rules button just right of the Game button */
  278.     rulesbutton = XtCreateManagedWidget("Rules", xmCascadeButtonWidgetClass, buttonpanel, NULL, 0);
  279.     rulesmenu   = XmCreatePulldownMenu(buttonpanel, "rulesmenu", NULL, 0);
  280.     {   struct rules **rp;
  281.     for (rp = rulepool; *rp; ++rp) {
  282.         Widget w;
  283.         w = XtCreateManagedWidget((*rp)->shortname, xmPushButtonWidgetClass, rulesmenu, NULL, 0);
  284.         XtAddCallback(w, XmNactivateCallback, selectrules, NULL);
  285.     }
  286.     }
  287.     XtVaSetValues(rulesbutton, XmNsubMenuId, rulesmenu, NULL);
  288.  
  289.     /* rest of the buttons */
  290.     for (bp = buttons, i = 0; i < XtNumber(buttons); ++i) {
  291.     Widget w;
  292.     w = XtCreateManagedWidget(bp->name, xmPushButtonWidgetClass, buttonpanel, NULL, 0);
  293.     if (bp->func) XtAddCallback(w, XmNactivateCallback, perform_command, (XtPointer)bp->func);
  294.     ++bp;
  295.     }
  296.  
  297. #ifdef SOUND
  298.     sound = XtCreateManagedWidget("Sound", xmToggleButtonWidgetClass, buttonpanel, NULL, 0);
  299. #endif
  300.  
  301.     dialog = XmCreateQuestionDialog(toplevel, "dialog", NULL, 0);
  302.     XtAddCallback(dialog, XmNokCallback, Ok, NULL);
  303.     XtAddCallback(dialog, XmNcancelCallback, Cancel, NULL);
  304.     {   Widget tmp;    /* throw away help button */
  305.     tmp = XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON);
  306.     XtUnmanageChild(tmp);
  307.     XtSetArg(Args[0], XmNhorizontalScrollBar, &sbh);
  308.     XtGetValues(container, Args, 1);
  309.     XtSetArg(Args[0], XmNverticalScrollBar, &sbv);
  310.     XtGetValues(container, Args, 1);
  311.     }
  312.  
  313. #ifdef ONLINE_HELP
  314.     create_help(toplevel);
  315. #endif
  316.     graphic.w_width = graphic.w_height = 0;
  317.     XtRealizeWidget(toplevel);
  318.     mainwindow = XtWindow(toplevel);
  319.     table  = XtWindow(desktop);
  320.  
  321.  
  322.     {
  323.     XSetWindowAttributes winattr;
  324.     long winmask;
  325.     winattr.backing_store = card.cardset ? WhenMapped : NotUseful;
  326.     winattr.border_pixel = blackpixel;
  327.     winattr.bit_gravity = NorthWestGravity;
  328.     winattr.event_mask = KeyPressMask | ExposureMask | ButtonPressMask | 
  329.         ButtonReleaseMask | StructureNotifyMask | Button3MotionMask;
  330.     winmask = CWBorderPixel | CWEventMask | CWBackingStore | CWBitGravity;
  331.     XChangeWindowAttributes(dpy, table, winmask, &winattr);
  332.     }
  333.  
  334.     graphics_control(Enable);
  335. #ifdef LABER
  336.     printf("main function requests init_layout:\n");
  337. #endif
  338.     init_layout();            /* position the piles in a nice way */
  339.     for (i = 0; i < game.numpiles; ++i)
  340.     pile_resize(i);
  341.     show_message(TXT_WELCOME, VERSION);
  342.     XStoreName(dpy, mainwindow, rules.longname ? rules.longname : rules.shortname);
  343.     if (!graphic.w_width) {
  344.     graphic.w_width = graphic.width;
  345.     graphic.w_height = graphic.height;
  346.     }
  347.     XtAppMainLoop(app_con);    /* does not return */
  348.     return 0;            /* keep compiler happy */
  349. }
  350.  
  351. void Force_Resize(XSize_t w, XSize_t h) {
  352.     /* for Xlib, we can use this size. It does include space for buttons and messagebox */
  353.     /* for Xaw, we have to add the size of the buttonbox and the message window */
  354.     /* note that the result may have a wrong height, due to recalculation of the box
  355.        widget. A second call to this function will always give the correct result. */
  356.     /* The overall size can be computed by adding the difference between the current
  357.        size of the mainwindow - current size of viewport window */
  358. #if 0
  359.     {   Arg args[1];
  360.         int hh, ww;
  361.     hh = 0; ww = 0;
  362.         XtSetArg(args[0], XmNheight, &hh);
  363.         XtGetValues(sbv, args, 1);
  364.         XtSetArg(args[0], XmNwidth, &ww);
  365.         XtGetValues(sbh, args, 1);
  366.     printf("viewport hat groesse %d,%d\n", ww, hh);
  367.         h += hh - graphic.w_height;    /* difference between overall size and Viewport size */
  368.     w += ww - graphic.w_width;
  369.     }
  370. #endif
  371.     w += 12;    /* quick, dirty */
  372.     h += 83;
  373.     XResizeWindow(dpy, mainwindow, w, h);
  374.     /* or should XSetWMSizeHints() be used? */
  375. }
  376. #endif
  377.